1056D - Decorate Apple Tree - CodeForces Solution


constructive algorithms dfs and similar dp graphs greedy sortings trees *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

#include <random>

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

#ifndef LOCAL
#pragma GCC optimize("unroll-loops,inline,Ofast")
// #pragma GCC target("avx2")
#endif

//#define int long long
//#define double long double
#define ont long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pint pair<int, int>
#define endl '\n'

template<class T>
bool cmax(T& a, T b) {
    if (a >= b) return false;
    a = b;
    return true;
}

template<class T>
bool cmin(T& a, T b) {
    if (a <= b) return false;
    a = b;
    return true;
}

vector<vector<int>> graph;
vector<int> leaf;

void dfs(int v) {
    for (auto& to : graph[v]) {
        dfs(to);
        leaf[v] += leaf[to];
    }
    leaf[v] += !leaf[v];
}

inline void solve() {
    int n;
    cin >> n;
    graph.resize(n);
    leaf.resize(n);
    for (int i = 1; i < n; ++i) {
        int x;
        cin >> x;
        graph[--x].push_back(i);
    }
    dfs(0);
    vector<int> ct(n);
    for (auto& x : leaf) ct[x]++;
    int i = 0;
    int sum = 0;
    for (int k = 1; k <= n; ++k) {
        while (sum < k) i++, sum += ct[i];
        cout << i << ' ';
    }
}

signed main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int ttt = 1;
//    cin >> ttt;
    while (ttt--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple